package com.nf.mvc.argument;

import com.nf.mvc.MethodArgumentResolver;
import com.nf.mvc.file.MultipartFile;
import com.nf.mvc.file.StandardMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.lang.reflect.Parameter;

public class MultipartFileMethodArgumentResolver implements MethodArgumentResolver {
    @Override
    public boolean supports(Parameter parameter) {
        return MultipartFile.class == parameter.getType();
    }

    @Override
    public Object resolveArgument(Parameter parameter, HttpServletRequest request) throws Exception {
        String partName = parameter.getName();
        Part part = request.getPart(partName);
        return new StandardMultipartFile(part);
    }
}
